const log4js = require("log4js");
const  logger = (funName) =>{
    log4js.configure({
        appenders:{
            console:{
                type:'dateFile',
                filename:`./logs/${funName}/${funName}.log`,
                pattern:'yyyy-MM-dd',
                // pattern:'yyyy-MM-dd-hh-mm-ss',
                keepFileExt: true,
                alwaysIncludePattern: true,
                layout:{
                    type: "pattern",
                    pattern:'{"date":"%d{yyyy-MM-dd hh:mm:ss}","type":"%p","data":"%m"} %n'
                }
            },
        },
        categories:{
            default: { appenders: ["console"], level: "all" },
            info:{
                appenders:["console"],
                level:"info"
            },
            error:{
                appenders:["console"],
                level:"error"
            },
            warn:{
                appenders:["console"],
                level:"warn"
            },
            debug:{
                appenders:["console"],
                level:"debug"
            },
            trace:{
                appenders:["console"],
                level:"trace"
            },
            'messageId':{
                appenders:["console"],
                level:"info"
            },
            'sendToErrorQueue':{
                appenders:["console"],
                level:"info"
            },
            'reqLog':{
                appenders:["console"],
                level:"info"
            },
            'resLog':{
                appenders:["console"],
                level:"info"
            },
            'reqNum':{
                appenders:["console"],
                level:"info"
            }
        }
    });
    return log4js.getLogger(funName);
}

const formatReqLog  =    (ctx, time) =>{
    let text = '------------request start------------'
    let method = ctx.method
    text += `request method: ${method} \n`

    if (method === 'GET') {
        text += `request data: ${JSON.stringify(ctx?.request?.query||{})} \n`; // 获取查询参数
    } else {
        text += `request data: ${JSON.stringify(ctx.body||{})} \n`; // 获取请求主体数据
    }
    // text += `ctx all: ${ctx}`
    text += `request time: ${time} \n`
    return text
}

const formatResLog  =   (response, time) =>{
    let text = '------------response start------------'
    text += `response result: ${response} \n`

    // text += `response all: ${ctx} \n`

    text += `response time: ${time} \n`
    return text
}

module.exports={
    logger: {
        info:(msg)=>{
            try{
                logger('info').info(msg);
            }catch (e) {
                console.log(e)
            }
        },
        error:(msg)=>{
            try{
                logger('error').error(msg);
            }catch (e) {
                console.log(e)
            }
        },
        warn:(msg)=>{
            try{
                logger('warn').warn(msg);
            }catch (e) {
                console.log(e)
            }
        },
        debug:(msg)=>{
            try{
                logger('debug').debug(msg);
            }catch (e) {
                console.log(e)
            }
        },
        trace:(msg)=>{
            try{
                logger('trace').trace(msg);
            }catch (e) {
                console.log(e)
            }
        },
        messageId:(msg)=>{
            try{
                logger('messageId').info(msg);
            }catch (e) {
                console.log(e)
            }
        },
        sendToErrorQueue:(msg) =>{
            try{
                logger('sendToErrorQueue').info(msg);
            }catch (e) {
                console.log(e)
            }
        },
        reqLog:(ctx,time)=>{
            try{
                logger('reqLog').info(formatReqLog(ctx,time));
            }catch (e) {
                console.log(e)
            }
        },
        resLog:(response,time)=>{
            try{
                logger('resLog').info(formatResLog(response,time));
            }catch (e) {
                console.log(e)
            }
        },
        reqNum:(msg)=>{
            try{
                logger('reqNum').info(msg);
            }catch (e) {
                console.log(e)
            }
        }
    }
}
